package com.wd.live.viewmodel

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.tencent.mm.opensdk.modelmsg.SendAuth
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.wd.live.tool.LocalData
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

/**
 * 登陆页面
 *@Author Nick.wu
 *Date 2023/8/14
 */
class LoginViewModel : BaseViewModel() {

    private val userInfoLiveData: MutableLiveData<LocalData.UserInfo> by lazy {
        MutableLiveData<LocalData.UserInfo>()
    }

    val userInfo: LiveData<LocalData.UserInfo>
        get() = userInfoLiveData

    fun getWxCode(wxApi: IWXAPI) {
        //拉起微信登录
        val req = SendAuth.Req()
        //授权域 获取用户个人信息则填写snsapi_userinfo
        req.scope = "snsapi_userinfo"
        //state用于保持请求和回调的状态，授权请求后原样带回给第三方。该参数可用于防止csrf攻击（跨站请求伪造攻击）
        // 建议第三方带上该参数，可设置为简单的随机数加session进行校验
        req.state = "test_login"
        //调用api接口，发送数据到微信
        wxApi.sendReq(req)
    }

    fun doLogin(code: String) {
        val params = HashMap<String, Any>()
        params["code"] = code
        systemApi?.wxLogin(params)?.let {
            it.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ bean ->
                    userInfoLiveData.value = bean.result
                    Log.d("provideSystemApi", bean.toString())
                }, { error ->
                    Log.d("provideSystemApi", error.message.toString())
                })
        }

    }

}